package com.fh.Idempotent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.RedisKey;
import utils.ResultObj;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("idempotent")
public class IdempotentController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping
    public ResultObj getToken(HttpServletRequest request){
        String token = UUID.randomUUID().toString();
        String idemKey = RedisKey.getIdemKey(token);
        redisTemplate.opsForValue().set(idemKey,System.currentTimeMillis(),2, TimeUnit.HOURS);
        return ResultObj.success(token);

    }
}
